package keshe.view;

import keshe.ACCOUNT;
import keshe.MyObjectInputStream;
import keshe.MyObjectOutputStream;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;

public class BankJFrame extends JFrame implements ActionListener {//银行界面
    private ACCOUNT account=new ACCOUNT();
    private keshe.view.AccountJPanel AccountJPanel;
    private JSplitPane jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);//水平分割窗格
    private JPanel right=new JPanel(new BorderLayout());//分割窗格右界面//边布局
    //建立银行账户列表
    private ArrayList<String>Names=new ArrayList<>();
    private ArrayList<ACCOUNT>accounts=new ArrayList<>();
    private DefaultListModel<String>NameModel=new DefaultListModel<>();
    private JList<String>NameList=new JList<>(NameModel);
    private JScrollPane scroll=new JScrollPane(NameList);
    private JPanel cmdPanel=new JPanel(new GridLayout(2,3));//命令面板
    private JButton in=new JButton("存款");//存款
    private JButton out=new JButton("取款");//取款
    private InOrOutJFrame inOrOutFrame;//存款和取款界面
    private JButton toOther=new JButton("转账");//转账
    private JButton OverLogin=new JButton("退出登录");//退出登录
    private JButton Cancellation=new JButton("销户");//销户
    private JButton Termination=new JButton("解除销户");//解除销户
    private JButton Refresh=new JButton("refresh");//刷新
    public BankJFrame(AccountJPanel ap,ACCOUNT account){
        //保存两个对象
        this.account=account;
        AccountJPanel=ap;

        this.setDefaultCloseOperation(3);
        //取消组件居中放置
        this.setLayout(null);
        this.setBounds(500,300,600,350);
        Refresh.setBounds(513,0,77,20);
        this.getContentPane().add(Refresh);//刷新按钮，刷新界面信息
        //分割窗格左界面添加个人账户界面
        jsp.setBounds(35,23,520,270);
        jsp.setOneTouchExpandable(true);//一键关闭和打开
        jsp.setDividerLocation(200);
        jsp.add(ap);
        this.getContentPane().add(jsp);

        //分割窗格右界面
        Right();
        //添加动作监听
        addAction();


        //添加背景
        JLabel ground=new JLabel(new ImageIcon("src/keshe/image/infobackgrand.jpg"));
        ground.setBounds(0,0,600,700);
        this.getContentPane().add(ground);
        this.setVisible(true);
    }

    private void Right() {

        //账户信息列表框
        ReadName();
        right.setBorder(new TitledBorder("银行用户信息"));
        right.add(scroll);
        jsp.add(right);
        //命令面版添加按钮
        cmdPanel.add(in);
        cmdPanel.add(out);
        cmdPanel.add(toOther);
        cmdPanel.add(OverLogin);
        cmdPanel.add(Cancellation);
        cmdPanel.add(Termination);
        //右面板下方添加命令面板
        right.add(cmdPanel,BorderLayout.SOUTH);
    }

    //读取文件中的全部账户信息
    private void ReadName() {
        try {
            MyObjectInputStream ois=new MyObjectInputStream(new FileInputStream("AccountData/Accounts"));
            while(true){
                try {
                    ACCOUNT account=new ACCOUNT();
                    account=(ACCOUNT)(ois.readObject());
                    accounts.add(account);
                    NameModel.addElement(account.getUserName()+"&&"+account.getBankCardId()+"   用户状态:"+account.getState()+"   余额:"+account.getMoney());
                } catch (EOFException e){
                    System.out.println("读取完成");
                    break;
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    private void addAction(){
        //刷新
        Refresh.addActionListener(e->{
            CINData(accounts);
            AccountJPanel accountJPanel=new AccountJPanel(account);
            this.dispose();
            new BankJFrame(accountJPanel,account);
        });
        //存款
        in.addActionListener(e->{
            if(account.getState()) {
                InOrOutJFrame ioo = new InOrOutJFrame(account,accounts);
                accounts=ioo.getAccounts();
            }else {
                Show("账户处于销户状态，请先解除销户！");
            }
        });

        //取款
        out.addActionListener(e->{
            if(account.getState()) {
                InOrOutJFrame ioo =new InOrOutJFrame(account,accounts);
                accounts=ioo.getAccounts();
            }else {
                Show("账户处于销户状态，请先解除销户！");
            }
        });

        //退出登录
        OverLogin.addActionListener(e->{
            int n=JOptionPane.showConfirmDialog(this,"确认退出？","退出登录",2);
            if(n==JOptionPane.OK_OPTION) {
                CINData(accounts);
                this.dispose();
                new LoginJFrame();
            }
        });

        //销户
        Cancellation.addActionListener(e->{
            if(!account.getState()){
                Show("您现在已经处于销户状态");
                return;
            }
            if(account.getMoney()!=0){
                Show("您的账户余额不为0，无法进行销户!");
                return;
            }
            int n=JOptionPane.showConfirmDialog(this,"确认销户？","销户",2);
            if(n==JOptionPane.OK_OPTION) {
                for (int i = 0; i < accounts.size(); i++) {
                    if(accounts.get(i).equals(account)){
                        accounts.get(i).setState(false);
                    }
                }
                Show("销户成功！");
                CINData(accounts);
                AccountJPanel accountJPanel=new AccountJPanel(account);
                this.dispose();
                new BankJFrame(accountJPanel,account);
            }
        });

        //解除销户
        Termination.addActionListener(e->{
            if(account.getState()){
                Show("您的账户处于未销户状态，无需解除销户");
            }
           int n=JOptionPane.showConfirmDialog(this,"确认解除销户？","解除销户",2);
                if(n==JOptionPane.OK_OPTION) {
                    for (int i = 0; i < accounts.size(); i++) {
                        if(accounts.get(i).equals(account)){
                            accounts.get(i).setState(true);
                        }
                    }
                    Show("解除销户成功!");
                    CINData(accounts);
                    AccountJPanel accountJPanel = new AccountJPanel(account);
                    this.dispose();
                    new BankJFrame(accountJPanel, account);
                }
        });

        //转账
        toOther.addActionListener(e->{
            if(!account.getState()){
                Show("您处于销户状态，不能进行转账操作");
                return;
            }
            new toOtherJFrame(account,accounts);
        });
    }
    private void CINData(ArrayList<ACCOUNT> accounts) {//将数据文件刷新
        MyObjectOutputStream oos= null;
        try {
            oos = new MyObjectOutputStream(new FileOutputStream("AccountData/Accounts",false));
            for (ACCOUNT account1:accounts) {
                oos.writeObject(account1);
            }
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void Show(String message) {
        JOptionPane.showMessageDialog(this,message);
    }
    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
